import java.io.File;
import java.util.Observable;

import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.DefaultFileMonitor;


public class FileChangedListener extends Observable implements FileListener {

    DefaultFileMonitor m_defaultFileMonitor;
    String m_filename;
    
    public FileChangedListener(String _filename) {
        m_filename = _filename;
    }
    
    public void start() throws FileSystemException {
        final FileSystemManager fsm = VFS.getManager();
        final FileObject fo = fsm.toFileObject(new File(m_filename));
        
        m_defaultFileMonitor = new DefaultFileMonitor(this);
        m_defaultFileMonitor.addFile(fo);
        m_defaultFileMonitor.start();
    }
    
    public void stop()
    {
    	m_defaultFileMonitor.stop();
    }
    
    public void fileChanged(FileChangeEvent _arg0) throws Exception {

        setChanged();
        notifyObservers();
    }

    public void fileCreated(FileChangeEvent _arg0) throws Exception {
        fileChanged(_arg0);
    }

    public void fileDeleted(FileChangeEvent _arg0) throws Exception {
        // Ne rien faire        
    }   
}
